package org.uu.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.uu.mybatisplus.dao.UserDao;
import org.uu.mybatisplus.mapper.UserMapper;
import org.uu.mybatisplus.service.IUserService;

import java.util.List;

@Slf4j
@Service
@CacheConfig(cacheNames = {"caffeineCacheManager", "redisCacheManager"} )
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDao> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Cacheable(value = "cacheManager")
    @Override
    public List<UserDao> getList() {
        return list();
    }

    @Override
    @CachePut(key = "#userDTO.id", value = "cacheManager")
    public boolean saveUser(UserDao userDAO) {
        return save(userDAO);
    }

    @Override
    @CachePut(key = "#userDTO.id", value = "cacheManager")
    public boolean update(UserDao userDAO) {
        return updateById(userDAO);
    }

    @Override
    @CacheEvict(key = "#id", value ="cacheManager" )
    public boolean deleteById(Long id) {
        return removeById(id);
    }

    @Cacheable(key = "#nickName", value = "cacheManager")
    @Override
    public List<UserDao> selectByNickname(String nickName) {
        LambdaQueryWrapper<UserDao> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserDao::getNickName, nickName);
        log.info("查询数据库");
        return userMapper.selectList(queryWrapper);
    }
}
